package com.edoomath.api.dao;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.course.CourseAttribute;
import com.edoomath.entity.course.CourseItem;
import com.edoomath.entity.course.CourseMaterail;
import com.edoomath.entity.course.CourseVersion;
import com.edoomath.entity.course.Sku;

/**
 * 
 * @author mohanlan
 *
 */
@Dao(value = "hibernate")
public interface CourseDao {
	
	/**
	 * 
	 * @param sku
	 * @return
	 */
	@Query("FROM Course WHERE sku = :sku AND disabled = false")
	public Course loadCourse(@Variable("sku") Sku sku);
    
    /**
     * 
     * @param course
     * @return
     */
    @Query("FROM CourseMaterail WHERE course = :course AND courseItem = null AND disabled = false ORDER BY type ASC, sorting ASC")
    public List<CourseMaterail> listMaterials(@Variable("course") Course course);
    
    /**
     * 
     * @param course
     * @param type
     * @return
     */
    @Query("FROM CourseMaterail WHERE course = :course AND courseItem IS NULL AND type = :type AND disabled = false ORDER BY type ASC, sorting ASC")
    public List<CourseMaterail> listMaterials(@Variable("course") Course course, @Variable("type") String type);
    
    /**
     * 
     * @param ids
     * @return
     */
    @Query("FROM CourseMaterail WHERE id in (:ids) AND disabled = false ORDER BY type ASC, sorting ASC")
    public List<CourseMaterail> listMaterials(@Variable("ids") List<Long> ids);
    
    /**
     * 
     * @param course
     * @return
     */
    @Query("FROM CourseMaterail WHERE course = :course AND courseItem IS NOT NULL AND disabled = false ORDER BY type ASC, sorting ASC")
    public List<CourseMaterail> listItemMaterials(@Variable("course") Course course);
    
    /**
     * 
     * @param course
     * @param type
     * @return
     */
    @Query("FROM CourseMaterail WHERE courseItem = :courseItem AND type = :type AND courseItem IS NOT NULL AND disabled = false ORDER BY courseItem.id ASC, type ASC, sorting ASC")
    public List<CourseMaterail> listItemMaterials(@Variable("courseItem") CourseItem courseItem, @Variable("type") String type);
    
    /**
     * 
     * @param course
     * @return
     */
    @Query("FROM CourseAttribute WHERE course = :course AND disabled = false")
    public List<CourseAttribute> listAttrs(@Variable("course") Course course);
    
    /**
     * 
     * @param courses
     * @return
     */
    @Query("FROM CourseAttribute WHERE course in (:courses) AND disabled = false")
    public List<CourseAttribute> listAttrs(@Variable("courses") List<Course> courses);
    
    /**
     * 
     * @param course
     * @param type
     * @return
     */
    @Query("FROM CourseMaterail WHERE course = :course AND courseItem IS NULL AND type = :type AND disabled = false ORDER BY type ASC, sorting ASC")
    public CourseMaterail loadMaterialsZip(@Variable("course") Course course, @Variable("type") String type);
    
    /**
     * 
     * @param course
     */
    @Query("FROM CourseVersion WHERE course = :course AND disabled = false AND latest = true")
    public CourseVersion loadVersion(@Variable("course") Course course);
    
}
